﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Metro.Kashikoi.ServiceContract.Security;
using Metro.Kashikoi.Service.Common.Entity;
using Metro.Kashikoi.Service.Security.Business;
using Metro.Kashikoi.ServiceContract.Location;

namespace Metro.Kashikoi.Service.Security.Service
{
    class SecurityService : ISecurityService
    {
        private IUnitOfWorkFactory Factory { get; set; }
        private ISecurityFacade SecurityFacade { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="SecurityService"/> class.
        /// </summary>
        public SecurityService()
        {
            this.Factory = new UnitOfWorkFactory();
        }

        /// <summary>
        /// Creates the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public void CreateUser(User user)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.CeaeteUser(ctx, user);
                ctx.SaveChanges();
            }

        }

        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="user">The user.</param>
        public void UpdateUser(User user)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.UpdateUser(ctx, user);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Deletes the user.
        /// </summary>
        /// <param name="id"></param>
        public void DeleteUser(int id)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.DeleteUser(ctx, id);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Gets all users.
        /// </summary>
        /// <returns></returns>
        public IList<User> GetAllUsers()
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                return SecurityFacade.GetAllUsers(ctx);
            }
        }

        /// <summary>
        /// Gets the user by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public User GetUserById(int id)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                return SecurityFacade.GetUserById(ctx, id);
            }
        }

        /// <summary>
        /// Validates the user.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public bool ValidateUser(string id, string password)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                return SecurityFacade.ValidateUser(ctx, id, password);
            }
        }

        /// <summary>
        /// Mantains the GRP id.
        /// </summary>
        /// <param name="user">The user.</param>
        public void MantainGrpId(User user)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.MantainGrpId(ctx, user);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Creates the user group.
        /// </summary>
        /// <param name="userGroup">The user group.</param>
        /// <returns></returns>
        public void CreateUserGroup(UserGroup userGroup)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.CeaeteUserGroup(ctx, userGroup);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Updates the user group.
        /// </summary>
        /// <param name="userGroup">The user group.</param>
        public void UpdateUserGroup(UserGroup userGroup)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.UpdateUserGroup(ctx, userGroup);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Deletes the user group.
        /// </summary>
        /// <param name="userGroupId">The user group id.</param>
        public void DeleteUserGroup(int userGroupId)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.DeleteUserGroup(ctx, userGroupId);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Gets all user groups.
        /// </summary>
        /// <returns></returns>
        public IList<UserGroup> GetAllUserGroups()
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                return SecurityFacade.GetAllUserGroups(ctx);
            }
        }

        /// <summary>
        /// Gets the user group by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public UserGroup GetUserGroupById(int id)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                return SecurityFacade.GetUserGroupById(ctx, id);
            }
        }

        /// <summary>
        /// Adds the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="userGroup">The user group.</param>
        public void AddUser(User user, UserGroup userGroup)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.AddUser(ctx, user, userGroup);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Removes the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="userGroup">The user group.</param>
        public void RemoveUser(User user, UserGroup userGroup)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.RemoveUser(ctx, user, userGroup);
                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Gets the user by login.
        /// </summary>
        /// <param name="login">The login.</param>
        /// <returns></returns>
        public User GetUserByLogin(string login)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                return SecurityFacade.GetUserByLogin(ctx, login);
            }
        }


        public IList<User> GetUserList(int userGroupId)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                return SecurityFacade.GetUserList(ctx, userGroupId);
            }
        }


        public void AddRoute(UserGroup userGroup, Region region)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.AddRoute(ctx, userGroup, region);
                ctx.SaveChanges();
            }
        }


        public void RemoveRoute(UserGroup userGroup, Region region)
        {
            using (IUnitOfWork ctx = Factory.GetContext<ISecurityService>())
            {
                SecurityFacade.RemoveRoute(ctx, userGroup, region);
                ctx.SaveChanges();
            }
        }

        #region ISecurityService Members


        public IList<UserGroup> GetUserGroupList(int userId)
        {
            throw new NotImplementedException();
        }

        public void AddUserToGroup(int userId, int userGroupId)
        {
            throw new NotImplementedException();
        }

        public void RemoveUserFromGroup(int userId, int userGroupId)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
